<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import { $t } from '@/locales';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { enableStatusOptions } from '@/constants/business';
import { translateOptions } from '@/utils/common';
import { fetchGetDeptTree } from '@/service/api';

defineOptions({
  name: 'PositionSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<Api.SystemManage.PostSearchParams>('model', { required: true });

/** the enabled dept tree options */
const deptTreeOptions = ref<CommonType.Option<number | null>[]>([]);

// 递归构建树形选项
function buildTreeOptions(deptList: Api.SystemManage.DeptTree[]): CommonType.Option<number | null>[] {
  return deptList.map(item => ({
    label: item.deptName,
    value: item.id,
    children: item.children && item.children.length > 0 ? buildTreeOptions(item.children) : undefined
  }));
}

async function getDeptTreeOptions() {
  const { error, data } = await fetchGetDeptTree();

  if (!error) {
    const treeOptions = buildTreeOptions(data);
    deptTreeOptions.value = [
      {
        label: $t('page.manage.position.allDept'),
        value: null
      },
      ...treeOptions
    ];
  }
}

async function reset() {
  await restoreValidation();
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}

onMounted(() => {
  getDeptTreeOptions();
});
</script>

<template>
  <NCard :title="$t('common.search')" :bordered="false" size="small" class="card-wrapper">
    <NForm ref="formRef" :model="model" label-placement="left" :label-width="80">
      <NGrid responsive="screen" item-responsive>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.manage.position.postId')" path="postId" class="pr-24px">
          <NInput v-model:value="model.postId" :placeholder="$t('page.manage.position.form.postId')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.manage.position.postName')" path="postName" class="pr-24px">
          <NInput v-model:value="model.postName" :placeholder="$t('page.manage.position.form.postName')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.manage.position.postCode')" path="postCode" class="pr-24px">
          <NInput v-model:value="model.postCode" :placeholder="$t('page.manage.position.form.postCode')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.manage.position.belongDept')" path="deptId" class="pr-24px">
          <NTreeSelect
            v-model:value="model.deptId"
            :options="deptTreeOptions"
            :placeholder="$t('page.manage.position.form.belongDept')"
            clearable
            filterable
            show-path
            separator=" / "
            key-field="value"
            label-field="label"
            children-field="children"
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.manage.position.status')" path="status" class="pr-24px">
          <NSelect
            v-model:value="model.status"
            :placeholder="$t('page.manage.position.form.status')"
            :options="translateOptions(enableStatusOptions)"
            clearable
          />
        </NFormItemGi>
        <NFormItemGi span="24 m:12" class="pr-24px">
          <NSpace class="w-full" justify="end">
            <NButton @click="reset">
              <template #icon>
                <icon-ic-round-refresh class="text-icon" />
              </template>
              {{ $t('common.reset') }}
            </NButton>
            <NButton type="primary" ghost @click="search">
              <template #icon>
                <icon-ic-round-search class="text-icon" />
              </template>
              {{ $t('common.search') }}
            </NButton>
          </NSpace>
        </NFormItemGi>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
